# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=gdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.2
pkgrel=3
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/gdb/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-xz")
options=('staticlibs' '!distcc' '!ccache')
source=(https://ftp.gnu.org/gnu/gdb/${_realname}-${pkgver}.tar.xz{,.sig}
        'gdb-perfomance.patch'
        'gdb-fix-using-gnu-print.patch'
        'gdb-7.12-dynamic-libs.patch'
        'python-configure-path-fixes.patch'
        'gdb-fix-tui-with-pdcurses.patch'
        'gdb-9.1-lib-order.patch'
        'gdb-home-is-userprofile.patch'
        'gdb-windows-aslr.patch'
        'gdb-windows-aslr-2.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('360cd7ae79b776988e89d8f9a01c985d0b1fa21c767a4295e5f88cb49175c555'
            'SKIP'
            'a0a2d974e80b656646c87cc669fda798182a35b001aab933567c41b880530aa0'
            '29fce74348a2862dd3856da58cd3c6c40a910cbdb95e5003d571c33a04532b78'
            '9de9c30bbb8cd2c6f60414b5a34cdf1fae27238293fb6007c628a8b821f88e0b'
            '6378e1a96e3bedc2a160f0f6780cb973ce6139017f2786e7ab97f8bcd9824d27'
            'f70cc0a0633d01adf777eb57a82f8c9880f6511d55e44c1dc415ddebc7467e0b'
            'dcfbfb2e3fc90a51e11202529e34c1b3dcc17e352067ffdfc58b63c1deab9523'
            'a63ba094dcd9bf0c0f98c6dce5825590b2cf7014834fd3bec5f88fe25d90228a'
            '7c40bd442dcd4eb32310b1b6979a96fd41253f4b3128ad0fac2f8ebff132666b'
            '394a041e799e9e0ac04e1a2f7455ec3567f1233059ed1432ac57174f23cc8026')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  # https://sourceware.org/bugzilla/show_bug.cgi?id=15412
  patch -p1 -i ${srcdir}/gdb-perfomance.patch

  patch -p1 -i ${srcdir}/gdb-fix-using-gnu-print.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=21078
  patch -p1 -i ${srcdir}/gdb-7.12-dynamic-libs.patch

  patch -p1 -i ${srcdir}/python-configure-path-fixes.patch

  patch -p1 -i ${srcdir}/gdb-fix-tui-with-pdcurses.patch
  patch -p1 -i ${srcdir}/gdb-9.1-lib-order.patch
  patch -p1 -i ${srcdir}/gdb-home-is-userprofile.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=26037
  patch -p1 -i ${srcdir}/gdb-windows-aslr.patch
  patch -p1 -i ${srcdir}/gdb-windows-aslr-2.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  
  if [ "${CARCH}" != "x86_64" ]; then
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
    CXXFLAGS+=" -O0"
  fi
  

  CPPFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  LDFLAGS+=" -fstack-protector"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-targets="i686-w64-mingw32,x86_64-w64-mingw32" \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --with-curses \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-system-readline \
    --with-python=${MINGW_PREFIX}/bin/python \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-zlib \
    --with-lzma \
    --enable-tui \
    --enable-source-highlight=no

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  rm -f ${pkgdir}${MINGW_PREFIX}/include/*.h
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a
}
